//
//  LGFBottomDatePickerView.swift
//  MedicalUnion
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 William_Xue. All rights reserved.
//

import UIKit

protocol LGFBottomDatePickerViewDelegate: class {
    func selectDate(view: LGFBottomDatePickerView, date: String, timestamp: Int)
}

class LGFBottomDatePickerView: UIView {
    
    weak var delegate: LGFBottomDatePickerViewDelegate?
    
    var bgView: UIView!
    
    var datePicker: UIDatePicker!
    
    var datePickerMode: UIDatePicker.Mode!
    
    var dateFormat: String!
    
    var callBack: ((LGFBottomDatePickerView?, String, Int) -> Void)!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showIn(_ SV: UIView, _ datePickerMode: UIDatePicker.Mode, _ dateFormat: String, _ callBack: @escaping (LGFBottomDatePickerView?, String, Int) -> Void) -> Void {
        self.datePickerMode = datePickerMode
        self.dateFormat = dateFormat
        self.callBack = callBack
        SV.addSubview(self)
        lgf_FillSuperview()
        
        config()
        
        bgView.transform = CGAffineTransform.init(translationX: 0, y: UIDevice.lgf_ScreenH / 2)
        DispatchQueue.main.async {
            UIView.animate(withDuration: 0.35) {
                self.bgView.transform = CGAffineTransform.identity
                self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
            }
        }
    }
    
    func config() -> Void {
        
        bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(UIDevice.lgf_ScreenH / 2.5)
        }
        
        let topView = UIView.init()
        bgView.addSubview(topView)
        topView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(50.lgf_H())
        }
        let leftButton = UIButton.init(type: .custom)
        leftButton.setTitle("取消", for: .normal)
        leftButton.titleLabel!.font = UIFont.boldSystemFont(ofSize: 16)
        leftButton.setTitleColor(UIColor.init(lgf_HexString: "0F88F2"), for: .normal)
        leftButton.addTarget(self, action: #selector(leftButtonSelect(_:)), for: .touchUpInside)
        topView.addSubview(leftButton)
        leftButton.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15.lgf_W())
            make.centerY.equalToSuperview()
        }
        
        let rightButton = UIButton.init(type: .custom)
        rightButton.setTitle("确认", for: .normal)
        rightButton.titleLabel!.font = UIFont.boldSystemFont(ofSize: 16)
        rightButton.setTitleColor(UIColor.init(lgf_HexString: "0F88F2"), for: .normal)
        rightButton.addTarget(self, action: #selector(rightButtonSelect(_:)), for: .touchUpInside)
        topView.addSubview(rightButton)
        rightButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15.lgf_W())
            make.centerY.equalToSuperview()
        }
       
        //----- 时间选择
        datePicker = UIDatePicker.init()
        if #available(iOS 13.4, *) {
            datePicker.preferredDatePickerStyle = .wheels
        } else {
            
        }
        datePicker.datePickerMode = datePickerMode
        datePicker.timeZone = TimeZone.current
        datePicker.locale = Locale.init(identifier: "zh_CN")
        bgView.addSubview(datePicker)
        datePicker.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(topView.snp.bottom)
        }

    }
    
    @objc func leftButtonSelect(_ sender: UIButton) -> Void {
        remove()
    }
    
    @objc func rightButtonSelect(_ sender: UIButton) -> Void {
        let dateFormater = DateFormatter.init()
        dateFormater.dateFormat = dateFormat
        let dateStr = dateFormater.string(from: datePicker.date)
        self.delegate?.selectDate(view: self, date: dateStr, timestamp: Int(datePicker.date.timeIntervalSince1970 * 1000))
        self.callBack(self, dateStr, Int(datePicker.date.timeIntervalSince1970 * 1000))
        remove()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first?.view == self {
            remove()
        }
    }
    
    func remove() -> Void {
        UIView.animate(withDuration: 0.35, animations: {
            self.bgView.transform = CGAffineTransform.init(translationX: 0, y: UIDevice.lgf_ScreenH / 2)
            self.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        }) { (finish) in
            self.removeFromSuperview()
        }
    }

}
